#!/usr/sbin/python
import csv,random
notes = {}

def start(daft):
	global notes
	notes = {}
	ready = csv.reader(open('./notes.csv'), delimiter=' ', quotechar='|')
	loadedrows = 0
	for row in ready:
		notes[row[0]] = row[1]
		loadedrows += 1
	print "loaded "+str(loadedrows)+" rows"

def store():
	global notes
	spamWriter = csv.writer(open('notes.csv', 'w'), delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
	savedrows = 0
	for notek in notes.keys():
		spamWriter.writerow([notek,notes[notek]])
		savedrows += 1
	return "saved "+str(savedrows)+" rows"

def add(key,val):
	global notes
	if notes.has_key(key):
		notes[key] = notes[key]+val+"; "
	else:
		notes[key] = val+"; "
	store()
	return str(len(notes[key].split("; "))-1) +" things about "+key+"."

def get(key,elsesay):
	global notes
	if notes.has_key(key):
		return random.choice(notes[key].split("; "))
	else:
		return elsesay

def literal(key,elsesay):
	global notes
	if notes.has_key(key):
		return notes[key]
	else:
		return elsesay


def drop(key,which):
	global notes
	old = notes[key].split("; ")
	if which != "all":
		say = "dropped "+old[which]+" from "+key+"."
	else:
		say = "dropped note "+key+"."
	new = ""
	for ind, ite in old:
		if ind != which and which != "all" and ite != which:
			new += ite+"; "
	notes[key] = new
	store()
	return say


def tick():
	pass
def start(daft):
	pass
def say(daft,r,who,w):
	pass
def stop():
	pass
